package com.mystyle.le.liuxiang.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.mystyle.le.liuxiang.R;
import com.mystyle.le.liuxiang.adapter.EntertainmentAdapter;
import com.mystyle.le.liuxiang.asynctaks.MyAsyncTaks;
import com.mystyle.le.liuxiang.beans.HeadList;
import com.mystyle.le.liuxiang.beans.HeadPager;
import com.mystyle.le.liuxiang.callback.MyCallBack;
import com.mystyle.le.liuxiang.utils.ContentUrls;
import com.mystyle.le.liuxiang.utils.ParseData;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 娱乐头条的fragment
 * A simple {@link Fragment} subclass.
 */
public class EntertainmentFragment extends Fragment {


    private ListView listView;
    private ViewPager Entertainment_viewpager;
    private List<View> viewList;

    public EntertainmentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_entertainment, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        listView = ((ListView) view.findViewById(R.id.listview));
        //添加头布局
        View inflate = getActivity().getLayoutInflater().inflate(R.layout.entertainment_head_layout, null);
        //初始化头布局控件
        initHeadView(inflate);
        listView.addHeaderView(inflate);

        new MyAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                List<HeadList> lists = ParseData.parseList(path);
                if (lists!=null&&lists.size()>0){
                    EntertainmentAdapter adapter = new EntertainmentAdapter(lists, getActivity());
                    listView.setAdapter(adapter);
                }
            }
        }, getActivity()).execute(ContentUrls.headLine_head);

        Entertainment_viewpager.setAdapter(new MyPagerAdaper());
    }

    /*
    * 头布局控件初始化,并赋值
    * */
    private void initHeadView(View inflate) {
        Entertainment_viewpager = ((ViewPager) inflate.findViewById(R.id.Entertainment_viewpager));
        //实例化
        viewList = new ArrayList<>();

        View inflate1 = getActivity().getLayoutInflater().inflate(R.layout.head_viewpager1, null);
        View inflate2 = getActivity().getLayoutInflater().inflate(R.layout.head_viewpager2, null);
        View inflate3 = getActivity().getLayoutInflater().inflate(R.layout.head_viewpager3, null);
        //初始化1
        initHeadTitle1(inflate1);
        initHeadTitle2(inflate2);
        initHeadTitle3(inflate3);

        viewList.add(inflate1);
        viewList.add(inflate2);
        viewList.add(inflate3);
    }

    private void initHeadTitle3(View inflate3) {
        final ImageView head_img1 = (ImageView) inflate3.findViewById(R.id.head_img3);
        final TextView head_tv1 = (TextView) inflate3.findViewById(R.id.head_tv3);
        getData(2,head_img1, head_tv1);
    }

    /*
    * 下载顶部滑动的图片数据
    * */
    private void getData(final int i, final ImageView head_img1, final TextView head_tv1) {
        new MyAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                if (path!=null){
                    List<HeadPager> list = ParseData.parseHeadPager(path);
                    if (list!=null&&list.size()>0){
                        HeadPager headPager = list.get(i);
                        head_tv1.setText(headPager.getTitle());
                        String img = headPager.getImg();
                        Picasso.with(getActivity()).load(img).into(head_img1);
                    }
                }
            }
        },getActivity()).execute(ContentUrls.headLine_head);
    }

    private void initHeadTitle2(View inflate2) {
        final ImageView head_img1 = (ImageView) inflate2.findViewById(R.id.head_img2);
        final TextView head_tv1 = (TextView) inflate2.findViewById(R.id.head_tv2);
        getData(1,head_img1, head_tv1);

    }

    private void initHeadTitle1(View inflate1) {
        final ImageView head_img1 = (ImageView) inflate1.findViewById(R.id.head_img1);
        final TextView head_tv1 = (TextView) inflate1.findViewById(R.id.head_tv1);
        getData(0,head_img1, head_tv1);
    }

    class MyPagerAdaper extends PagerAdapter{

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
}
